require 'rubygems'
require 'ferret'
require 'anemone'
require 'open-uri'
#require 'fileutils'

include Ferret

class Crawl

	@@bulas_index = Index::Index.new(:path => './bulario.idx')  
	@@dominio = "http://bulario.net"
	
    def self.with_depth(depth)
		#if File.exist?('./bulario.idx')
		#	FileUtils.rm_rf './bulario.idx'
		#end

		print "\n"
		print " " + "URL".ljust(40)
		print "Profundidade".ljust(10) + "\n"
		puts "-" * 54

		Anemone.crawl(@@dominio, :depth_limit => depth.to_i) do | anemone |  

			anemone.on_every_page do | page |  
				@@bulas_index << {  
					:url => page.url,  
					:drug => page.doc.at('title').text,  
					:indications => page.doc.css('p').text  
				}  
				line = " " << page.url.to_s.ljust(40)
				line << " " + page.depth.to_s.center(10)
				puts line
			end  

		end  
    end

end
